package com.xiguaji.tiktok.v1.ins.brand.controller;

import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.InsBaseController;
import com.xiguaji.tiktok.v1.ins.brand.dto.BrandMediaSearchRes;
import com.xiguaji.tiktok.v1.ins.brand.dto.InsBrandResp;
import com.xiguaji.tiktok.v1.ins.brand.dto.InsBrandSearchRes;
import com.xiguaji.tiktok.v1.ins.brand.service.InsBrandService;
import com.xiguaji.tiktok.v1.ins.favorite.dto.FavoriteTypeEnum;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.service.InsFavoriteService;
import com.xiguaji.tiktok.v1.product.dto.PlatformTypeEnum;
import com.xiguaji.tiktok.v1.product.dto.ReportTypeEnum;
import com.xiguaji.tiktok.v1.product.service.ProductService;
import com.xiguaji.tiktok.v1.tiktok.permission.Limit;
import com.xiguaji.tiktok.v1.tiktok.permission.RequestLimit;
import com.xiguaji.tiktok.v1.user.log.Operation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController("InsBrandControllerApi")
@Api(value = "ins品牌",tags = {"ins品牌"})
public class InsBrandController extends InsBaseController {

    @Autowired
    InsBrandService insBrandService;

    @Autowired
    ProductService productPerService;

    @Autowired
    InsFavoriteService insFavoriteService;



    @ApiOperation(value = "获取INS品牌列表")
    @RequestMapping(value = "brand",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @RequestLimit(value = "InsBrandSearchPageLimit",property = "page,pageSize",validateType = Limit.VIEW_DATA_LIMIT)
    @Operation(value = "获取INS品牌列表")
    public String getMediaPage(String data) throws UnsupportedEncodingException, ParamsException {
        InsBrandSearchRes insBrandSearchRes = getParam(InsBrandSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        Page<InsBrandResp> insBrandRespPage = insBrandService.getPage(insBrandSearchRes);
        insBrandRespPage.getList().forEach( brand ->{
            InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_BRAND.getCode(),brand.getId(),(int)getUser().getId());

            if(null != insFavorite) {
                brand.setCollectId(insFavorite.getId());
            }
        });
        return Ret.success("SUCCESS",insBrandRespPage);
    }


    @ApiOperation(value = "获取INS品牌列表")
    @RequestMapping(value = "brand/search",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "获取INS品牌列表")
    public String searchMediaPage(String data) throws UnsupportedEncodingException, ParamsException {
        InsBrandSearchRes insBrandSearchRes = getParam(InsBrandSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        if(insBrandSearchRes.getPage()*insBrandSearchRes.getPageSize()>10){
            return Ret.noPermission();
        }
        return Ret.success("SUCCESS",insBrandService.getPage(insBrandSearchRes));
    }

    @ApiOperation(value = "统计品牌下的帖子信息")
    @RequestMapping(value = "brand/media/st",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "统计品牌下的帖子信息")
    public String getBrandStMedia(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandStMedia(res.getBrandId(),res.getDayType()));
    }

    @ApiOperation(value = "统计品牌下的帖子发文走趋")
    @RequestMapping(value = "brand/media/trend",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "统计品牌下的帖子发文走趋")
    public String getBrandStMediaTrend(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandStMediaTrend(res.getBrandId(),res.getDayType()));
    }


    @ApiOperation(value = "品牌下的帖子分页")
    @RequestMapping(value = "brand/media",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌下的帖子分页")
    public String getBrandMediaPage(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandMediaPage(res));
    }

    @ApiOperation(value = "品牌下达人的帖子分页")
    @RequestMapping(value = "brand/blogger/media",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌下达人的帖子分页")
    public String getBrandBloggerMediaPage(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandBloggerMediaPage(res));
    }

    @ApiOperation(value = "统计品牌下的商品帖子信息")
    @RequestMapping(value = "brand/product/media/st",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "统计品牌下的商品帖子信息")
    public String getBrandProductStMedia(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandProductSt(res.getBrandId(),res.getDayType()));
    }

    @ApiOperation(value = "统计品牌下的商品帖子发文走趋")
    @RequestMapping(value = "brand/product/media/trend",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "统计品牌下的商品帖子发文走趋")
    public String getBrandProductStMediaTrend(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandProductStTrend(res.getBrandId(),res.getDayType()));
    }


    @ApiOperation(value = "品牌下的商品分类统计")
    @RequestMapping(value = "brand/product/category",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌下的商品分类统计")
    public String getBrandProductCategory(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandProductCategoryCount(res.getBrandId(),res.getDayType()));
    }


    @ApiOperation(value = "品牌下的商品分页")
    @RequestMapping(value = "brand/product",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌下的商品分页")
    public String getBrandProductPage(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandProductPage(res));
    }

    /**
     * 品牌账户概览
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌账户概览")
    @RequestMapping(value = "brand/account/overview",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌账户概览")
    public String getBrandAccountOverview(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandAccountOverview(res.getBrandId(),res.getDayType()));
    }

    /**
     * 品牌账户商业分类
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌账户商业分类")
    @RequestMapping(value = "brand/account/industry",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌账户商业分类")
    public String getBrandAccountIndustry(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandAccountIndustry(res.getBrandId()));
    }

    /**
     * 品牌账户粉丝级别
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌账户粉丝级别")
    @RequestMapping(value = "brand/account/followerSize",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌账户粉丝级别")
    public String getBrandAccountFollowerSize(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandAccountFollowerSize(res.getBrandId()));
    }

    /**
     * 品牌账户列表
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌账户列表")
    @RequestMapping(value = "brand/account/list",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌账户列表")
    public String getBrandAccountList(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandAccountList(res.getBrandId(),res.getDayType(),res.getPage(),res.getPageSize()));
    }

    /**
     * 品牌达人分析统计信息
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌达人分析统计信息")
    @RequestMapping(value = "brand/ad/account/count",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌达人分析统计信息")
    public String getBrandAdInfluencerCount(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandAdInfluencerCount(res.getBrandId(),res.getDayType()));
    }

    /**
     * 品牌达人分析周走趋
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌达人分析周走趋")
    @RequestMapping(value = "brand/ad/account/trend",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌达人分析周走趋")
    public String getBrandAdInfluencerOfWeekDay(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getBrandAdInfluencerOfWeekDay(res.getBrandId(),res.getDayType()));
    }

    /**
     * 品牌达人分析达人列表
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParamsException
     */
    @ApiOperation(value = "品牌达人分析达人列表")
    @RequestMapping(value = "brand/ad/account/page",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌达人分析达人列表")
    public String getAdInfluencerPage(String data) throws UnsupportedEncodingException, ParamsException {
        BrandMediaSearchRes res = getParam(BrandMediaSearchRes.class, URLDecoder.decode(data,"UTF-8"));
        return Ret.success("SUCCESS",insBrandService.getAdInfluencerPage(res.getBrandId(),res.getDayType(),res.getPage(),res.getPageSize()));
    }



    /**
     *
     * @return
     */
    @ApiOperation(value = "验证是否可以解锁报告")
    @RequestMapping(value = "brand/report/unlock/checked",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "验证是否可以解锁报告")
    public String checkedUnlockedBloggerReport(){
        String msg = "";
        Map<String,Object> result = new HashMap<>();
        if(getRoleId() >2){
            int roleReportNumber = productPerService.unlockedNumberByRole(getUser().getId(),getRoleId(),ReportTypeEnum.BRAND.getMsg());
            msg = "Your daily reports balance is "+roleReportNumber+" reports.";
            if(roleReportNumber > 0){
                msg += " Unlock this report costs 1 daily report. Do you want to continue?";
                result.put("type",1);
                return Ret.success(msg,result);
            }
        }
        int reportStarterNumber = productPerService.unlockedNumberByReportStarter(getUser().getId());

        if(reportStarterNumber > 0){
            msg += "Your credits reports balance is "+reportStarterNumber+". Unlock this report costs 1 credit. Do you want to continue?";
            result.put("type",2);
            return Ret.success(msg,result);
        }
        return Ret.success("Insufficient daily reports. Please try again tomorrow or purchase credits.");
    }

    /**
     *
     * @param body
     * @return
     * @throws ParamsException
     */
    @ApiOperation(value = "解锁报告")
    @RequestMapping(value = "brand/report/unlock",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @Operation(value = "解锁报告")
    public String unlockedBloggerReport(@RequestBody String body) throws ParamsException {
        Long uid = getParam(Long.class,body,"bid");
        if(productPerService.isUnlocked(getUser().getId(),uid, PlatformTypeEnum.INS.getCode())){
            return Ret.success("你已解锁过了");
        }

        if(productPerService.unlockedByUserRole(getUser().getId(),getRoleId(),uid,PlatformTypeEnum.INS.getCode(), ReportTypeEnum.BRAND.getCode(), ReportTypeEnum.BRAND.getMsg())){
            return Ret.success("解锁成功");
        }

        if(productPerService.unlockedByReportStarter(getUser().getId(),uid, PlatformTypeEnum.INS.getCode(), ReportTypeEnum.BRAND.getCode())){
            return Ret.success("解锁成功");
        }
        return Ret.err("解锁失败");
    }

    //=========================== 品牌粉丝画像 ===========================
    @ApiOperation(value = "品牌粉丝年龄性别占比")
    @RequestMapping(value = "brand/fans/{bid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌粉丝年龄性别占比")
    public String getBrandFansGender(@PathVariable int bid){
        return Ret.success("SUCCESS",insBrandService.getBrandFansGender(bid));
    }
    @ApiOperation(value = "品牌粉丝年龄段")
    @RequestMapping(value = "brand/fans/age/{bid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌粉丝年龄段")
    public String getBrandFansGenerations(@PathVariable int bid){
        return Ret.success("SUCCESS",insBrandService.getBrandFansGenerations(bid));
    }
    @ApiOperation(value = "品牌粉丝国家分布")
    @RequestMapping(value = "brand/fans/nation/{bid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌粉丝国家分布")
    public String getBrandFansNations(@PathVariable int bid) {
        return Ret.success("SUCCESS",insBrandService.getBrandFansNations(bid));
    }
    @ApiOperation(value = "品牌粉丝语言分布")
    @RequestMapping(value = "brand/fans/language/{bid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌粉丝语言分布")
    public String getBrandFansLanguages(@PathVariable int bid){
        return Ret.success("SUCCESS",insBrandService.getBrandFansLanguages(bid));
    }
    @ApiOperation(value = "品牌粉丝人种分布")
    @RequestMapping(value = "brand/fans/race/{bid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @Operation(value = "品牌粉丝人种分布")
    public String getBrandFansRaces(@PathVariable int bid){
        return Ret.success("SUCCESS",insBrandService.getBrandFansRaces(bid));
    }
}
